using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace YF.Art
{
    public class DTCheckTexture : EditorWindow
    {
        string path = "Assets/";
        Vector2 scrollView;
        int w = 128;
        int h = 128;
        int winhight = 500;
        List<Texture2D> _pendingRenamePaths = new List<Texture2D>();
        [MenuItem("DT Tools /Check Texture Size")]
        private static void ShowWindow()
        {
            var window = GetWindow<DTCheckTexture>();
            window.titleContent = new GUIContent("CheckTexture");
            window.Show();
        }

        List<string> outid = new List<string>();
        private void OnGUI()
        {
            winhight = (int)this.position.height - 130;
            path = EditorGUILayout.TextField("path", path);
            w = EditorGUILayout.IntField("w", w);
            h = EditorGUILayout.IntField("h", h);
            if (GUILayout.Button("Check"))  // 检查按钮
            {
                string[] guids = AssetDatabase.FindAssets("t:Texture2D", new string[] { path });
                _pendingRenamePaths.Clear();
                foreach (string guid in guids)  // 遍历所有纹理
                {
                    string assetPath = AssetDatabase.GUIDToAssetPath(guid);  // 获取纹理路径
                    Texture2D importer = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath) as Texture2D;  // 获取纹理导入器

                    if (importer == null) continue;  // 如果导入器为空，跳过  
                    int width = importer.width;  // 获取纹理宽度
                    int height = importer.height;  // 获取纹理高度
                    if (width > w || height > h)  // 如果纹理尺寸大于512x512
                    {
                        _pendingRenamePaths.Add(importer);  // 标记需要重命名的纹理
                    }
                }
            }
            if (GUILayout.Button("Export"))  // 重命名按钮
            {
                outid.Clear();
                Debug.Log("_pendingRenamePaths:" + _pendingRenamePaths.Count);
                foreach (var texture in _pendingRenamePaths)
                {
                    string[] ss = AssetDatabase.GetAssetPath(texture).Split('/');
                    int index = ss.Length - 3;
                    outid.Add(ss[index]);
                }
                Debug.Log("outid:" + string.Join(",", outid));

            }
            if (_pendingRenamePaths.Count > 0)
            {
                EditorGUILayout.LabelField("需要检查的纹理：");  // 显示需要重命名的纹理列表 
                scrollView = EditorGUILayout.BeginScrollView(scrollView, GUILayout.Height(winhight));
                foreach (var texture in _pendingRenamePaths)
                {
                    EditorGUILayout.BeginVertical();
                    EditorGUILayout.SelectableLabel(AssetDatabase.GetAssetPath(texture));  // 显示纹理名称
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.ObjectField(texture, typeof(Texture2D), false);  // 显示纹理对象
                    if (texture != null)
                    {
                        EditorGUILayout.LabelField(texture.width.ToString() + "x" + texture.height.ToString(), GUILayout.MaxWidth(100));  // 显示纹理尺寸
                    }
                    EditorGUILayout.EndHorizontal();
                    GUILayout.Button("", GUILayout.Height(2));
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndScrollView();
            }
        }
    }
}
